package com.zx.BookSeaReader.greendao.service;

import com.zx.BookSeaReader.greendao.GreenDaoManager;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookListBean;
import com.zx.BookSeaReader.greendao.entity.BookListCollect;
import com.zx.BookSeaReader.greendao.entity.BookListSubscribe;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.gen.BookListBeanDao;
import com.zx.BookSeaReader.greendao.gen.BookListCollectDao;
import com.zx.BookSeaReader.greendao.gen.BookListSubscribeDao;
import com.zx.BookSeaReader.util.Constant;
import com.zx.BookSeaReader.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ZX
 * @Time: 2020/4/25
 * @Description: 功能描述
 */
public class BookListService extends BaseService {

    private UserService mUserService;

    public BookListService(){
        mUserService =new UserService();
    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询所有书单
     */
    public List<BookListBean> getAllBookLists(){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.loadAll();
        return bookListBeans;

    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 订阅书单
     */
    public void addSubscribeBookList(BookListBean bookList, UserBean userBean){
        BookListSubscribe bookListSubscribe=new BookListSubscribe();
        bookListSubscribe.setBookListid(bookList.getListId());
        bookListSubscribe.setUserid(userBean.getUserid());
        BookListSubscribeDao bookListSubscribeDao = GreenDaoManager.getInstance().getSession().getBookListSubscribeDao();
        bookListSubscribeDao.insert(bookListSubscribe);

    }

    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询我的书单
     */
    public List<BookListBean> getMyBookLists(UserBean userBean){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.Username.eq(userBean.getUsername()))
                .list();

        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }

    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询书单内容
     */
    public List<Book> getBookListBooks(BookListBean bookListBean){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        BookListBean bookList =  bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.ListId.eq(bookListBean.getListId()))
                .unique();
        List<Book> books=bookList.getBookList();

        return books;
    }

    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询除我以外所有书单
     */
    public List<BookListBean> getBookListsWithoutMine(UserBean userBean){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.Username.notEq(userBean.getUsername()),BookListBeanDao.Properties.IsRelease.eq(true))
                .orderDesc(BookListBeanDao.Properties.CreateTime)
                .list();

        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }


    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 删除订阅书单
     */
    public void deleteBookListSub(BookListBean bookListBean,UserBean userBean){
        BookListSubscribeDao bookListSubscribeDao = GreenDaoManager.getInstance().getSession().getBookListSubscribeDao();
        List<BookListSubscribe> bookListSubscribes=bookListSubscribeDao.queryBuilder()
                .where(BookListSubscribeDao.Properties.Userid.eq(userBean.getUserid()),BookListSubscribeDao.Properties.BookListid.eq(bookListBean.getListId()))
                .list();
        bookListSubscribeDao.deleteInTx(bookListSubscribes);

        if(bookListBean.getUsername().equals(userBean.getUsername())){
            deleteBookListCollect(bookListBean);
            deleteEntity(bookListBean);

        }
    }

    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 删除订阅
     */
    public void deleteSubBookList(BookListBean bookListBean,UserBean userBean){
        BookListSubscribeDao bookListSubscribeDao = GreenDaoManager.getInstance().getSession().getBookListSubscribeDao();
        List<BookListSubscribe> bookListSubscribes=bookListSubscribeDao.queryBuilder()
                .where(BookListSubscribeDao.Properties.Userid.eq(userBean.getUserid()),BookListSubscribeDao.Properties.BookListid.eq(bookListBean.getListId()))
                .list();
        bookListSubscribeDao.deleteInTx(bookListSubscribes);
    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 是否订阅
     */
    public boolean isBookListSub(BookListBean bookListBean,UserBean userBean){
        BookListSubscribeDao bookListSubscribeDao = GreenDaoManager.getInstance().getSession().getBookListSubscribeDao();
        List<BookListSubscribe> bookListSubscribes=bookListSubscribeDao.queryBuilder()
                .where(BookListSubscribeDao.Properties.Userid.eq(userBean.getUserid()),BookListSubscribeDao.Properties.BookListid.eq(bookListBean.getListId()))
                .list();

        if(bookListSubscribes.size()>0){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 取消书本连接
     */
    public void deleteBookListCollect(BookListBean bookListBean){
        BookListCollectDao bookListCollectDao = GreenDaoManager.getInstance().getSession().getBookListCollectDao();
        List<BookListCollect> bookListCollects=bookListCollectDao.queryBuilder()
                .where(BookListCollectDao.Properties.BookListid.eq(bookListBean.getListId()))
                .list();
        bookListCollectDao.deleteInTx(bookListCollects);
    }


    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询可查所有书单
     */
    public List<BookListBean> getAllVisibleBookLists(){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.IsRelease.eq(true))
                .orderDesc(BookListBeanDao.Properties.CreateTime)
                .list();
        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 模糊查询书单时间排序
     */
    public List<BookListBean> getLikeBookListsByTime(String key){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.IsRelease.eq(true),BookListBeanDao.Properties.Title.like("%" + key + "%"))
                .orderDesc(BookListBeanDao.Properties.CreateTime)
                .list();
        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 模糊查询书单点赞排序
     */
    public List<BookListBean> getLikeBookListsByLoveCount(String key){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.IsRelease.eq(true),BookListBeanDao.Properties.Title.like("%" + key + "%"))
                .orderDesc(BookListBeanDao.Properties.CollectorCount)
                .list();
        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询可查所有书单
     */
    public List<BookListBean> getAllVisibleBookListsByLoveCount(){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.IsRelease.eq(true))
                .orderDesc(BookListBeanDao.Properties.CollectorCount)
                .list();
        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }
    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 查询订阅书单
     */
    public List<BookListBean> getSubBookLists(UserBean userBean){
        UserBean user=mUserService.getUser(userBean.getUsername());
        List<BookListBean> bookListBeans = new ArrayList<>();
        bookListBeans = user.getBookListBeans();
        if(bookListBeans.size()>0){
            return bookListBeans;
        }else {
            bookListBeans=new ArrayList<>();
            return bookListBeans;
        }

    }



    /**
     * @Author: ZX
     * @Time: 2020/4/25
     * @Description: 添加书单
     */
    public void addBookList(List<Book> books, UserBean userBean, String title, String info, Boolean release){
        BookListBean bookList = new BookListBean();
        bookList.setCover(books.get(0).getImgUrl());
        bookList.setBookCount(books.size());
        bookList.setTitle(title);
        bookList.setDesc(info);
        bookList.setUsername(userBean.getUsername());
        bookList.setCreateTime(StringUtils.
                dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
        bookList.setCollectorCount(0);
        bookList.setIsRelease(release);

        addEntity(bookList);

        bookList = getBookList(userBean,title);

        for(Book book:books){
            book.setIsLocal(false);
            addBookListCollect(book,bookList);
        }
        updateEntity(bookList);
    }
    /**
     * 新增书单书本连接
     * @param
     */
    public void addBookListCollect(Book book, BookListBean bookListBean){
        BookListCollect bookListCollect=new BookListCollect();
        bookListCollect.setBookid(book.getId());
        bookListCollect.setBookListid(bookListBean.getListId());
        BookListCollectDao bookListCollectDao = GreenDaoManager.getInstance().getSession().getBookListCollectDao();
        bookListCollectDao.insert(bookListCollect);
    }
    /**
     * 查找书单
     * @param
     */
    public BookListBean getBookList(UserBean userBean, String title){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.Username.eq(userBean.getUsername()),BookListBeanDao.Properties.Title.eq(title))
                .list();

        if(bookListBeans.size()>0){
            return bookListBeans.get(0);
        }else {
            //bookListBeans=new
            return null;
        }
    }
    /**
     * 查找书单
     * @param
     */
    public boolean existBookList(UserBean userBean, String title){
        BookListBeanDao bookListBeanDao = GreenDaoManager.getInstance().getSession().getBookListBeanDao();
        List<BookListBean> bookListBeans=bookListBeanDao.queryBuilder()
                .where(BookListBeanDao.Properties.Username.eq(userBean.getUsername()),BookListBeanDao.Properties.Title.eq(title))
                .list();

        if(bookListBeans.size()>0){
            return true;
        }else {
            //bookListBeans=new
            return false;
        }
    }

}
